package com.xuanwei.admin.interceptor;

import com.alibaba.fastjson.JSON;
import com.xuanwei.core.response.ResultVO;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author jiangyong
 * @Description TODO 未登录返回的json数据
 * @Date 2019/4/15 11:26
 **/
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json");
        httpServletResponse.getWriter().write(JSON.toJSONString(ResultVO.LoginFail()));
        httpServletResponse.getWriter().flush();
        httpServletResponse.getWriter().close();
    }
}
